package com.lpl.web.controllers;

import com.lpl.polo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Alissues")
public class Alissues {

    @Autowired
    private com.lpl.service.Issues Issues;
//    获取科目
@GetMapping("/get")
public result getAlissues(@RequestParam(value = "name", required = false) String name) {
    if ("null".equals(name)||name == null || name.isEmpty()) {
        name = null;
    }


    List<Question> res = Issues.serIssues(name);
    return result.success(res);
}


//获取详情
    @GetMapping("/getdetails")
    public result getAlissgetdetails(@RequestParam int id){
        List<Question>  res= Issues.getdetails(id);
        return result.success(res);
    }



    //  新增科目
    @PostMapping("/xizneng")
    public result xizneng(@RequestBody kemu Question){
        System.out.println("sss");
        System.out.println(Question+"sss");
  Issues.insertimu(Question);
        return result.success("添加成功");
    }

    //  删除科目
    @GetMapping("/shanchu")
    public result xizneng(String id){


        Issues.shanchu(id);
        return result.success("删除成功");
    }



    //  编辑科目
    @PostMapping("/bianji")
    public result bianji(@RequestBody kemu data){
System.out.println(data);

        Issues.baijji(data);
        return result.success("删除成功");
    }

    //  获取题目
    @GetMapping("/gettimu")
    public result gettimu(@RequestParam(required = false) String ipunt,
                          @RequestParam(required = false) Integer status) {
        // 处理参数为空的情况
        if (ipunt != null && ipunt.trim().isEmpty()) {
            ipunt = null;
        }
        if (status == null) {
            status = null;
        }
//
//        // 调用业务逻辑方法获取题目数据
        List<Question> u = Issues.gettimu(new tishai(ipunt, status));
        return result.success(u);
    }







    //  新增题目
    @PostMapping("/isetimu")
    public result isetimu(@RequestBody newq data){
           Issues.isetimu(data);
        Integer generatedId = data.getId();
        List<Option> U=data.getOptions();
        if(generatedId!=null){
              //新增选项
            for (Option item : U) {
                item.setBigt_id(generatedId);

                Issues.isexuan(item);
            }
        }
        return result.success("新增成功");
    }

    //  修改题目
    @PostMapping("/xiuisetimu")
    public result xiutimu(@RequestBody newq data) {
        Issues.xiutimu(data);
        Integer generatedId = data.getId();

        List<Option> U = data.getOptions();
        if (generatedId != null) {
            //修改选项
            for (Option item : U) {
                item.setBigt_id(generatedId);

                if(item.getId()!=null){
                    Issues.xiuxuan(item);

                }else{
                    Issues.isexuan(item);
                }
            }

        }
return  result.success();
    }


    //  根据id查找选项
    @GetMapping("/getIdxian")
    public result getIdxian(Integer id){


        List<Option> u= Issues.getxuan(id);
        return result.success(u);
    }

    //  根据id删除选项
    @GetMapping("/shanIdxian")
    public result shanIdxian(Integer id){


        try {
           int u= Issues.shangetxuanx(id);
           System.out.println(u);
           if(u!=0){

           }
            Issues.shangetxuan(id);

            return result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息，便于排查问题
            return result.error("删除失败！"); // 返回错误信息给前端
        }
    }

    //  添加图片
    @PostMapping("/tianjiatu")
    public result tiatupian(@RequestBody img data) {

    System.out.println(data);
        Issues.tianjiatu(data);


        return  result.success("成功");
    }
    // 查查找图片
    @GetMapping("/gettupian")
    public result tiatupian() {


       List<img> u= Issues.getjiatu();


        return  result.success(u);
    }



    @GetMapping("/shantupian")
    public result shanchu(int id) {


        List<img> u= Issues.shanjiatu(id);


        return  result.success(u);
    }


}
